from dataclasses import dataclass
import re


@dataclass
class ZQResult:
    model_name: str
    time_main_fmi2DoStep: float
    time_jmi_ode_cvode_solve: float
    f_evaluations: int
    non_linear_iteration: int
    numsteps: int
    steps: int
    fevals: int
    netfails: int
    nniters: int
    nncfails: int
    total_time: float
    param_num: int


def parse_zq_results(filepath):
    with open(filepath, 'r') as file:
        content = file.read()

    def find_float(pattern):
        """ Helper function to search and convert found pattern to float. """
        match = re.search(pattern, content)
        return float(match.group(1)) if match else None

    def find_int(pattern):
        """ Helper function to search and convert found pattern to integer. """
        match = re.search(pattern, content)
        return int(match.group(1)) if match else None

    model_name = filepath.split("\\")[-1].replace(".txt", "")
    # Extract data using regex
    data = ZQResult(
        model_name=model_name,
        time_main_fmi2DoStep=find_float(r"time_main_fmi2DoStep: ([\d\.]+) s"),
        time_jmi_ode_cvode_solve=find_float(r"time_jmi_ode_cvode_solve: ([\d\.]+) s"),
        f_evaluations=find_int(r"f_evaluations: (\d+)"),
        non_linear_iteration=find_int(r"non_linear_iteration: (\d+)"),
        numsteps=find_int(r"numsteps: (\d+)"),
        steps=find_int(r"steps: (\d+)"),
        fevals=find_int(r"fevals: (\d+)"),
        netfails=find_int(r"netfails: (\d+)"),
        nniters=find_int(r"nniters: (\d+)"),
        nncfails=find_int(r"nncfails: (\d+)"),
        total_time=find_float(r"total_time:\s+([\d\.]+)s"),
        param_num=find_int(r"param num: (\d+)")
    )

    return data


# Example Usage
if __name__ == "__main__":
    result_file_path = r"D:\pyProject\dymola_test\dymola_test\500-500\zq_n_res\Modelica.Fluid.Examples.HeatingSystem.txt"
    result = parse_zq_results(result_file_path)
    print(result)
